/*
 * Copyright 2006 Robert Burrell Donkin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package rat.anttasks;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;

import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.types.resources.FileResource;
import org.apache.tools.ant.util.FileUtils;

import rat.element.ArchiveElement;
import rat.element.BinaryElement;
import rat.element.ElementTypeEnum;
import rat.element.IElement;
import rat.element.IElementConsumer;
import rat.element.IElementContainer;
import rat.element.NoteElement;
import rat.element.StandardContentElement;

/**
 * Implementation of IElement that traverses over a resource
 * collection internally.
 */
class ResourceCollectionContainer implements IElementContainer {
    private final ResourceCollection rc;

    ResourceCollectionContainer(ResourceCollection rc) {
        this.rc = rc;
    }

    public void consumeContents(IElementConsumer consumer) {

        for (Iterator iter = rc.iterator(); iter.hasNext(); ) {
            Resource r = (Resource) iter.next();
            String name = r.getName();
            if (r.isDirectory()) {
                // do nothing
            } else {
                FileResource fr = null;
                if (r instanceof FileResource) {
                    fr = (FileResource) r;
                }

                ElementTypeEnum guess = ElementTypeEnum.guessType(name);

                if (guess == ElementTypeEnum.NOTE) {
                    consumer.consume(new NoteElement(name));
                } else if (guess == ElementTypeEnum.BINARY) {
                    consumer.consume(new BinaryElement(name));
                } else if (guess == ElementTypeEnum.ARCHIVE) {
                    if (fr != null) {
                        consumer.consume(new ArchiveElement(fr.getFile()));
                    } else {
                        consumer.consume(new ArchiveElement(r.getName(), null));
                    }
                } else if (guess == ElementTypeEnum.STANDARD_CONTENT) {
                    consumer.consume(getStandardContent(r, fr));
                } else {
                    InputStream in = null;
                    try {
                        if (BinaryElement.isBinary(in = r.getInputStream())) {
                            consumer.consume(new BinaryElement(name));
                            continue;
                        }
                    } catch (IOException ex) {
                        // swallow
                    } finally {
                        FileUtils.close(in);
                    }
                    consumer.consume(getStandardContent(r, fr));
                }
            }
        }
    }



    private IElement getStandardContent(Resource r, FileResource fr) {
        IElement element = null;
        if (fr != null) {
            element = new StandardContentElement(fr.getFile());
        } else {
            try {
                element = new StandardContentElement(r.getName(),
                                     new InputStreamReader(r.getInputStream()));
            } catch (IOException ex) {
                // swallow
            }
        }
        return element;
    }
}